// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : object.index

private alias object.value2 : object.drawX
private alias object.value3 : object.drawY
private alias object.value4 : object.letter
private alias object.value5 : object.charamount
private alias object.value6 : Object.MenuChoose

private alias object.value7 : object.fadetimer

private alias object.value8 : Object.TouchTimer

private alias object.value9 : Object.Controls

private alias object.value10 : Object.MenuDirection

public alias saveRAM[295] : saved.mobile
public alias saveram[1987] : saved.Mouse
public alias saveRAM[286] : saved.language

public value BoxPositionY = 0;
public value CharacterTalking = 0;
public value TextActive = 0;
public value CutsceneActive = 0;

reserve function TextDrawing

function TextDrawing

	temp0 = object.drawX
	temp1 = object.charamount
	while temp1 > 0
	// Get the actual character at the current char pos
	GetTextInfo(object.letter, 1, TEXTINFO_TEXTDATA, Object.Index, ArrayPos0)

	Object.Frame = 0
	
	// Take uppercase letters and turn them into a matching Sprite Frame
	if object.letter > 64
		if object.letter < 91
			Object.Frame  = object.letter
			Object.Frame -= 64
		end if
	end if
	
	// Take lowercase letters and turn them into a matching Sprite Frame
	if object.letter > 96
		if object.letter < 123
			Object.Frame  = object.letter
			Object.Frame -= 96
		end if
	end if

		//ASCII Table required!
		switch object.letter
			case 33	//!
				ArrayPos0++
				temp1--
				object.drawX += 0x69
				break		
			case 39
				// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
				
				DrawSpriteScreenFX(44, FX_INK, object.drawX, object.drawY)

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				object.drawX += 0x8
			break
			case 48	;DrawSpriteScreenFX(44, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//0
			case 49	;DrawSpriteScreenFX(45, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//1
			case 50	;DrawSpriteScreenFX(46, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//2
			case 51	;DrawSpriteScreenFX(47, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//3
			case 52	;DrawSpriteScreenFX(48, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 53	;DrawSpriteScreenFX(49, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 54	;DrawSpriteScreenFX(50, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 55	;DrawSpriteScreenFX(51, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 56	;DrawSpriteScreenFX(52, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 57	;DrawSpriteScreenFX(53, FX_INK, object.drawX, object.drawY) ;ArrayPos0++ ;temp1-- ;object.drawX += 0x8 ;break	//4

			case 64	//@	LINE SKIPPER
				object.drawY += 16
				object.drawX = temp0
			

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
			break
			default
			// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
			

			DrawSpriteScreenFX(Object.Frame, FX_INK, object.drawX, object.drawY)

			// Move to the next letter
			ArrayPos0++
			
			// And decrease the remaining letter count, as well
			temp1--
			
			// Each letter should be spaced 8 pixels apart
			object.drawX += 0x8
			break
		end switch
	loop
end function

//if being stupid was a job

event ObjectMain
	if stage.actnum != 1
		object[2].type = TypeName[Blank Object]
		object[3].type = TypeName[Blank Object]
		object[4].type = TypeName[Blank Object]		
	end if


	switch object.state
	case 0
		object.timer++
		object.fadetimer = 255
		Object.Index = 0
		TextActive = 1
		object.charamount = 0
		if object.timer == 100
			object.state++
			object.timer = 0
		end if	
	break
	case 1
		if object.fadetimer > -1
			object.fadetimer -= 8
		else
			object.fadetimer = 0	
		end if
		SetClassicFade(0, 0, 0, Object.fadetimer)	

		if object.alpha < 255
			object.alpha += 12
		end if

		if inputpress.start == 1
			object.charamount = 70
			object.state++
		end if	


		if saved.language == 0
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Object.Index, 0)
			if object.charamount == 75
				object.timer++
				if object.timer == 100
					object.state++
					object.timer = 0
				end if
			else
				PlaySfx(SfxName[Score Add], 0)	
				object.charamount++
			end if
		else
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Object.Index, 0)
			if object.charamount == temp0
				object.timer++
				if object.timer == 200
					object.state = 3
					object.timer = 0
				end if
			else
				PlaySfx(SfxName[Score Add], 0)	
				object.charamount++
			end if
		end if
	break
	case 2
		object.alpha = 255
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Object.Index, 0)
		if object.charamount == temp0
			object.timer++
			if object.timer == 150
				object.state++
				object.timer = 0
			end if
		else	
			PlaySfx(SfxName[Score Add], 0)	
			object.charamount++
		end if

		if inputpress.start == 1
			object.charamount = temp0
		end if	

	break
	case 3
		if object.alpha > 0
			object.alpha -= 8
		end if		

	
		object.timer++
		if object.timer == 75
			if Object[3].value5 >= 99
				Object.Index = 3
			else
				rand(temp3, 2)
				temp3 += 5
				Object.Index = temp3
			end if	
			object.charamount = 0			
			object.state++
			object.timer = 0
		end if
	break
	case 4
		if object.alpha < 255
			object.alpha += 8
		end if		


			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult == -1
				Object.TouchTimer = false
			end if
			if Object.TouchTimer == 0
				//LEFT BUTTONS
				temp0 = object.value9
				temp0 += screen.xsize
				temp0 -= screen.xsize
				temp1 = object.value9
				temp1 += screen.xsize
				temp1 -= 225
				CheckTouchRect(temp0, 90, temp1, 110)
				if CheckResult > -1
					Object.TouchTimer = 1
					Object.MenuChoose = 0
					inputpress.start = 1
				end if

				CheckTouchRect(temp0, 112, temp1, 135)
				if CheckResult > -1
					Object.TouchTimer = 1
					Object.MenuChoose = 1
					inputpress.start = 1
				end if				

				CheckTouchRect(temp0, 137, temp1, 155)
				if CheckResult > -1
					Object.TouchTimer = 1
					Object.MenuChoose = 2
					inputpress.start = 1
				end if				

				CheckTouchRect(temp0, 157, temp1, 177)
				if CheckResult > -1
					Object.TouchTimer = 1
					Object.MenuChoose = 3
					inputpress.start = 1
				end if	

				if saveRAM[6001] == 1
					temp0 = object.value9
					temp0 += screen.xsize
					temp0 -= screen.xsize
					temp1 = object.value9
					temp1 += screen.xsize
					temp1 -= 225
					CheckTouchRect(temp0, 180, temp1, 200)
					if CheckResult > -1
						Object.TouchTimer = 1
						Object.MenuChoose = 4
						inputpress.start = 1
					end if	

					temp0 = object.value9
					temp0 += screen.xsize
					temp0 -= screen.xsize
					temp1 = object.value9
					temp1 += screen.xsize
					temp1 -= 225
					CheckTouchRect(temp0, 203, temp1, 224)
					if CheckResult > -1
						Object.TouchTimer = 1
						Object.MenuChoose = 5
						inputpress.start = 1
					end if	
				end if
			end if
					

		
		if saveRAM[6001] == 0
			if inputpress.down == 1
				if Object.MenuChoose > 2
					Object.MenuChoose = 0
				else	
					Object.MenuChoose++
				end if
				PlaySfx(SfxName[Score Add], 0)			
			end if

			if inputpress.up == 1
				if Object.MenuChoose < 1
					Object.MenuChoose = 3
				else	
					Object.MenuChoose--
				end if
				PlaySfx(SfxName[Score Add], 0)			
			end if
		else
			if inputpress.down == 1
				if Object.MenuChoose > 4
					Object.MenuChoose = 0
				else	
					Object.MenuChoose++
				end if
				PlaySfx(SfxName[Score Add], 0)			
			end if

			if inputpress.up == 1
				if Object.MenuChoose < 1
					Object.MenuChoose = 5
				else	
					Object.MenuChoose--
				end if
				PlaySfx(SfxName[Score Add], 0)			
			end if
		end if


		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Object.Index, 0)
		if object.charamount < temp0
			object.charamount++
		end if

		if inputpress.start == 1
			PlaySfx(SfxName[Lamp Post], 0)
			switch Object.MenuChoose
			case 0
				options.gameMode = 1
				object[3].state = 97
			break
			case 1
				options.gameMode = 1
				saveRAM[6000] = 1
				object[3].state = 98
			break	
			case 2
				object.state = 5
			break
			case 3
				object[3].state = 99
			break
			case 4
				options.gameMode = 1
				object[3].state = 96
			break	
			case 5
				options.gameMode = 1
				object[3].state = 95
			break						
			endswitch
		endif

		if inputpress.buttonB == 1
			object[3].state = 99
		end if



		//para android

	break
	case 6
		temp0 = 0
		temp1 = 80
		CheckTouchRect(temp0, 0, temp1, 82)
		if CheckResult > -1
			inputPress.buttonB = 1
		end if

		
		
		if inputpress.right == 1
			saved.Mouse = 1
			PlaySfx(SfxName[Score Add], 0)	
		end if

		if inputpress.left == 1
			saved.Mouse = 0
			PlaySfx(SfxName[Score Add], 0)	
		end if

	break
	end switch
	

end event

event ObjectDraw

	object.inkEffect = INK_ALPHA

	ArrayPos0 = 0	//starting letter

	switch object.state
	case 0
	case 1
	case 2
	case 3
		object.drawY = 100
		
		temp3 = screen.xsize
		temp3 -= screen.xcenter
		temp3 -= 130

		object.drawX = temp3

		temp1 = screen.xcenter
		temp1 -= 28

		DrawSpriteScreenFX(34, FX_INK, temp1, 30)
	break
	case 4
	case 5
		//DrawSpriteScreenFX(39, FX_INK, screen.xcenter, 30)

		if object.state == 5
			if object.alpha > 0
				object.alpha -= 8
			else
				object.alpha = 0
				object.state++
			end if
		end if

		object.drawY = 50
		
		temp3 = screen.xsize
		temp3 -= screen.xcenter
		temp3 -= 140
		object.drawX = temp3

		DrawSpriteScreenFX(35, FX_INK, temp3, 100)


		//beat night 5
		if saveRAM[6001] == 1
			//menu + night 6
			DrawSpriteScreenFX(41, FX_INK, temp3, 169)
			//custom night
			DrawSpriteScreenFX(60, FX_INK, temp3, 212)
		else
			DrawSpriteScreenFX(59, FX_INK, temp3, 169)			
		end if

		switch Object.MenuChoose
			case 0	//new Game
				DrawSpriteScreenFX(36, FX_INK, temp3, 100)
			break
			case 1	//Continue
				DrawSpriteScreenFX(37, FX_INK, temp3, 123)
			break
			case 2	//Controls
				DrawSpriteScreenFX(38, FX_INK, temp3, 146)
			break	
			case 3	//Main Menu
				DrawSpriteScreenFX(58, FX_INK, temp3, 169)
			break		
			case 4	//Night 6
				DrawSpriteScreenFX(42, FX_INK, temp3, 191)
			break		
			case 5	//Custom Night
				DrawSpriteScreenFX(43, FX_INK, temp3, 212)
			break	
		end switch



		temp3 += 70
		DrawSpriteScreenFX(40, FX_INK, temp3, 104)	
	
		if saveRAM[6001] == 1
			temp3 -= 90
			DrawSpriteScreenFX(56, FX_INK, temp3, 45)
		end if

		//beat night 6
		if saveRAM[6002] == 1
			temp3 += 30
			DrawSpriteScreenFX(56, FX_INK, temp3, 45)
		end if		

		//beat night 6
		if saveRAM[6003] == 1
			temp3 += 30
			DrawSpriteScreenFX(56, FX_INK, temp3, 45)
		end if		

	break
	case 6
		if object.alpha < 255
			object.alpha += 8
		else
			object.alpha = 255
		end if	

		if inputPress.buttonB == 1
			object.state++
		end if
		if saved.mobile == 0
			DrawSpriteScreenFX(61, FX_INK, screen.xcenter, screen.ycenter)
		else
			DrawSpriteScreenFX(63, FX_INK, screen.xcenter, screen.ycenter)
		end if

		if saved.mobile == 0
			DrawSpriteScreenFX(64, FX_INK, screen.xcenter, 32)

			
			temp1 = screen.xcenter
			temp1 += 42
			
			if saved.Mouse == 0
				DrawSpriteScreenFX(65, FX_INK, temp1, 32)
			else
				DrawSpriteScreenFX(66, FX_INK, temp1, 32)
			end if
			DrawSpriteScreenFX(67, FX_INK, temp1, 32)
		end if



		if saved.mobile == 1
			DrawSpriteScreenFX(62, FX_INK, 8, 12)
		end if
	break
	case 7

		if object.alpha > 0
			object.alpha -= 8
		else
			object.alpha = 0
			object.state = 4
		end if	

		if saved.mobile == 0
			DrawSpriteScreenFX(61, FX_INK, screen.xcenter, screen.ycenter)
		else
			DrawSpriteScreenFX(63, FX_INK, screen.xcenter, screen.ycenter)
		end if
	break
	end switch

	CallFunction(TextDrawing)

	if object.state == 0
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 255)
	end if




end event

event ObjectStartup
	LoadSpriteSheet("FNAF/Objects1.gif")
	switch saved.language
		case 0
			LoadTextFile(1, "Data/Game/FreddyMenu.txt")
		break
		case 1	//PTBR
			LoadTextFile(1, "Data/Game/FreddyMenu.txt")
		break
		case 2 //ES
			LoadTextFile(1, "Data/Game/Spanish/FreddyMenu.txt")
		break
		case 3 //French
			LoadTextFile(1, "Data/Game/French/FreddyMenuFR.txt")
		break
	end switch
	SpriteFrame(0, 0, 0, 0, 0, 0)	//0 the bar

	// #1 -> #34 - Alphabet
	// (Using loops to automate Sprite Framing)

	//not related too stuff in objectdraw

	//temp7: starting ypos, progresses by 17 and stops before 100, because theres only 3 rows
	//temp6: starting xpos, the letter A
	temp1 = 49
	while temp1 < 100
		temp0 = 549
		while temp0 < 648
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	//fred head
	SpriteFrame(-3, -6, 64, 64, 513, 117) //35

	//options
	SpriteFrame(-3, -6, 64, 57, 888, 1) //36

	//continue, new game, main menu
	SpriteFrame(-3, -6, 64, 11, 956, 1) //37
	SpriteFrame(-3, -6, 64, 11, 956, 24) //38
	SpriteFrame(-3, -6, 64, 11, 956, 47) //39

	SpriteFrame(-3, -6, 202, 217, 821, 560) //40

	SpriteFrame(-3, -6, 40, 7, 847, 1) //41	

	SpriteFrame(-3, -6, 91, 34, 888, 59) //42

	SpriteFrame(-3, -6, 91, 11, 888, 94) //43

	SpriteFrame(-3, -6, 91, 11, 888, 117) //44





	//#45 - idk numbers
	temp1 = 49
	while temp1 < 100
		temp0 = 513
		while temp0 < 549
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	SpriteFrame(-3, -6, 64, 64, 513, 182) //56

	//new main menu stuff
	SpriteFrame(-3, -6, 64, 11, 688, 117) //57	
	SpriteFrame(-3, -6, 64, 11, 756, 117) //58

	SpriteFrame(-3, -6, 91, 11, 888, 59) //59	


	SpriteFrame(-3, -6, 91, 11, 732, 105) //60		

	//controls
	SpriteFrame(-160, -64, 322, 149, 701, 129) //61		

	SpriteFrame(-3, -6, 64, 64, 578, 182) //62

	//controls
	SpriteFrame(-160, -72, 316, 149, 675, 279) //63	

	SpriteFrame(-64, 0, 72, 11, 732, 93) //64

	SpriteFrame(-11, 0, 23, 11, 824, 93) //65
	SpriteFrame(-8, 0, 16, 11, 824, 81) //66

	SpriteFrame(-21, 0, 41, 11, 815, 69) //67

	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[2].Type = TypeName[Message Text]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[2].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[2].DrawOrder = 6

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
